package com.twu.biblioteca;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Scanner;

class Biblioteca {
    private static JSONArray books;

    public Biblioteca() throws JSONException {
        books = new JSONArray("[{'name':'Head First Java','author':'Wang', 'published': '1909', 'status': '1'}," +
                                "{'name':'TDD by Example','author':'Amos', 'published': '1949', 'status': '1'}]");
    }

    public void listBooks() throws JSONException {
        System.out.println("List of Books");
        System.out.println("        " + "Name" + "              " + "Author" + "       " + "published");

        for(int i = 0; i < books.length(); ++i){
            JSONObject book = books.getJSONObject(i);
                if(book.getString("status").equals("1")){
                    System.out.print((i + 1) + " ");
                    System.out.print(book.getString("name") + "          ");
                    System.out.print(book.getString("author") + "          ");
                    System.out.println(book.getString("published"));
                }
        }
        System.out.println("Please choose your choice");
        System.out.println("1、 checkout a book");
        System.out.println("2、 return a book");
        Scanner sc = new Scanner(System.in);
        int command = sc.nextInt();
        if(command == 1)
            checkoutBookDialog();
        else
            returnBookDialog();

    }

    public void checkoutBookDialog() throws JSONException {
        System.out.println("\ncheckout a book ?");
        Scanner sc = new Scanner(System.in);
        int command = sc.nextInt();
        if(command > 0 && command <= books.length() && books.getJSONObject(command-1).getString("status").equals("1")){
            checkoutBook(books.getJSONObject(command - 1));
            listBooks();
        }
        else {
            System.out.println("That book is not available.");
        }

    }

    public void returnBookDialog() throws JSONException {
        System.out.println("\nreturn a book ?");
        Scanner sc = new Scanner(System.in);
        int command = sc.nextInt();
        if(command > 0 && command <= books.length() && books.getJSONObject(command-1).getString("status").equals("0")){
            returnBook(books.getJSONObject(command - 1));
            listBooks();
        }
        else {
            System.out.println("That is not a valid book to return.");
        }

    }

    public void checkoutBook(JSONObject book) throws JSONException {
        book.put("status", "0");
        System.out.println("Thank you! Enjoy the book");
    }

    public void returnBook(JSONObject book) throws JSONException {
        book.put("status", "1");
        System.out.println("Thank you for returning the book.");
    }

}



public class BibliotecaApp {

    public static void main(String[] args) throws IOException, JSONException {
        Biblioteca b = new Biblioteca();
        Scanner sc = new Scanner(System.in);
        System.out.println("******Welcome To Biblioteca******\n\n");
        System.out.println("       1、List Books");
        System.out.println("       0、Quit");
        int command = sc.nextInt();
        while(command != 0){
            if(command == 1){
                b.listBooks();
                break;
            }
            else{
                System.out.println("Select a valid option!");
                command = sc.nextInt();
            }
        }

    }
}
